package com.swpu.service_knowledge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swpu.service_knowledge.entity.KnowledgeArticle;
import com.swpu.service_knowledge.entity.KnowledgeCategory;
import com.swpu.service_knowledge.entity.vo.ArticleCollectVo;
import com.swpu.service_knowledge.entity.vo.KnowledgeArticleVo;
import com.swpu.service_knowledge.mapper.KnowledgeArticleMapper;
import com.swpu.service_knowledge.service.KnowledgeArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swpu.service_knowledge.service.KnowledgeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-03-27
 */
@Service
public class KnowledgeArticleServiceImpl extends ServiceImpl<KnowledgeArticleMapper, KnowledgeArticle> implements KnowledgeArticleService {
    @Autowired
    private KnowledgeCategoryService knowledgeCategoryService;
    @Override
    public List<KnowledgeArticle> getKnowledgeArticleByCategoryId(Integer id) {
        LambdaQueryWrapper<KnowledgeArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KnowledgeArticle::getCategoryId,id).eq(KnowledgeArticle::getStatus,1);
        return this.list(queryWrapper);
    }

    @Override
    public List<KnowledgeArticleVo> getAllKnowledgeArticle() {
        List<KnowledgeCategory> categoryList = knowledgeCategoryService.getCategoryList();

        return null;
    }

    @Override
    public KnowledgeArticle getKnowledgeArticleById(Integer id) {
        KnowledgeArticle knowledgeArticle = this.getById(id);
        knowledgeArticle.setHot(knowledgeArticle.getHot()+1);
        this.updateById(knowledgeArticle);
        return knowledgeArticle;
    }

    @Override
    public List<ArticleCollectVo> getUserArticleCollectList(String openid) {
        List<ArticleCollectVo> articleCollectVoList = baseMapper.getArticleCollectByUserId(openid);
        return articleCollectVoList;
    }

    @Override
    public List<KnowledgeArticle> getHotKnowledgeArticle() {
        Page<KnowledgeArticle> articlePage = new Page<>(1,10);
        LambdaQueryWrapper<KnowledgeArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(KnowledgeArticle::getHot);
        this.page(articlePage,queryWrapper);
        return articlePage.getRecords();
    }

    @Override
    public List<KnowledgeArticle> getKnowledgeArticleByTitle(String title) {
        LambdaQueryWrapper<KnowledgeArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(KnowledgeArticle::getArticleTitle,title);
        return this.list(queryWrapper);
    }
}
